package
{
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.sampler.NewObjectSample;
	import flash.system.TouchscreenType;
	import gl.gestures.GestureFlick;
	import gl.touches.TouchDown;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import XML;
	import flash.net.URLLoader;
	import flash.display.MovieClip;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.display.Bitmap;
	import flash.net.navigateToURL;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import com.greensock.*;
	import com.greensock.easing.*
	import id.core.Application;
	import id.core.TouchSprite;
	import gl.events.TouchEvent;
	import gl.events.GestureEvent
	import flash.utils.setTimeout;
	import com.greensock.plugins.TweenPlugin;
	import com.greensock.plugins.AutoAlphaPlugin;
	
	/**
	 * ...
	 * @author Aydarka
	 */
	public class Main extends Application
	{
		private var Cathegory_list:Array = new Array();
		private var imageMain_list:Array = new Array();
		private var imageMainTxt_list:Array = new Array();
		private var image_list:Array = new Array();
		private var bigImage_list:Array = new Array();
		private var title_list:Array = new Array();
		private var txt_list:Array = new Array();
		
		private var i:Number;
		private var tn:Number = 0;
		private var scale_factor:Number = 0.6;
		private var tween_duration:Number = 0.8;
		
		private var angle:Number;
		private var degree:Number
		private var total:Number;
		private var totalPic:Number;
		private var flashmo_xml:XML = new XML();
		private var folder:String = "thumbnails/";
		private var xml_loader:URLLoader = new URLLoader();
		private var thumbnail_group:TouchSprite;
		private var default_y:Number;
		private var flashmo_mc:TouchSprite;
		
		private var loaderName:String;
		private var bigpicLoader:Loader;
		private var loaderStats:TouchEvent;
		private var Xint:Number;
		private var FlickCheker:Number = 0;
		private var Cicl:Number = 0;
		private var NextEvent:GestureEvent;
		var tweenNext:TweenLite;
		var thumbsource:String;
		var thumb:Picture;
		var mainSprite:TouchSprite = new TouchSprite;
		var pileTouch:TouchSprite;
		var targetpile:TouchSprite;
		var targetPhoto:TouchSprite;
		var targetphotoX:Number;
		var targetphotoY:Number;
		var targetphotoR:Number;
		var targetInd:Number = 0;
		var pileMain:TouchSprite
		var photoTouch:TouchSprite;
		var targetPic:TouchSprite;
		var targetPic2:TouchSprite;
		var X:Array = new Array();
		var Y:Array = new Array();
		var mc_speedXTO: Array=new Array();
var mc_speedYTO: Array=new Array();
var mc_speedXFROM: Array=new Array();
var mc_speedYFROM: Array=new Array();
var speed:Number = 20;
private var mousePrevX:Number = 0;
		private var mousePrevY:Number= 0;
		private var arrayX:Array;
		private var arrayY:Array;
		private var prevPhoto:TouchSprite

		public function Main():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			addChild(mainSprite);
			xml_loader.load(new URLRequest("flashmo_129_thumbnail_list.xml"));
			xml_loader.addEventListener(Event.COMPLETE, create_thumbnail);
			
			setTimeout(load, 1000);
			
			TweenPlugin.activate([AutoAlphaPlugin]);
			//	this.addEventListener(GestureEvent.GESTURE_FLICK_1, to_next);
			//	addEventListener(TouchEvent.TOUCH_MOVE, rotate);
		
		}
		
		private function create_thumbnail(e:Event):void
		{
			flashmo_xml = XML(e.target.data);
			total = flashmo_xml.cathegory.length();
	
			for (i = 0; i < total; i++)
			{
				var ph:XMLList = flashmo_xml.cathegory[i].pics
			
				image_list = []
				for (var k in ph)
				{
					;
					thumbsource = folder + ph[k].@url;
					//trace("УРЛ такой  " + (folder + ph[k].@url))
					thumb = new Picture(thumbsource, k);
					image_list.push(thumb);
					
				}
				imageMain_list.push(image_list);
				
			}
		
		}
		
		public function load():void
		{
			for (var j:int = 0; j < imageMain_list.length; j++)
			{
				pileMain = new TouchSprite;
				//pileMain.x = (300 * j)+100
				//pileMain.y=100
				pileMain.name = "pileMain" + j
				
				if (j < 3)
				{
					pileMain.x = (j * 300) + 100;
					pileMain.y = 100;
				}
				else
				{
					pileMain.y = mainSprite.getChildByName("pileMain" + (j - 3)).y + 200;
					pileMain.x = mainSprite.getChildByName("pileMain" + (j - 3)).x;
				}
				
				mainSprite.addChild(pileMain);
				pileMain.addEventListener(TouchEvent.TOUCH_TAP, touchChildre);
				for (var z:int = 0; z < imageMain_list[j].length; z++)
				{
					var arrayObj:TouchSprite = new TouchSprite
					arrayObj = imageMain_list[j][z];
					arrayObj.rotation = Math.random() * (70 * Math.random()) * 150;
					arrayObj.x = Math.random() * 20;
					arrayObj.y = Math.random() * 20;
					pileMain.addChild(arrayObj);
					
				}
				
			}
		}
		
		private function touchChildre(e:TouchEvent):void
		{
			
			for (var j:int = 0; j < mainSprite.numChildren; j++)
			{
				pileTouch = mainSprite.getChildByName("pileMain" + j) as TouchSprite;
				if (pileTouch.name !== e.currentTarget.name)
				{
					TweenLite.to(pileTouch, 1, {autoAlpha: 0, ease: Back.easeOut});
					
				}
			}
			targetpile = e.currentTarget as TouchSprite;
			//trace(targetpile.name);
		
			targetpile.removeEventListener(TouchEvent.TOUCH_TAP, touchChildre);
			arrayX = [];
				arrayY = [];
			
			TweenLite.to(targetpile, 3, {x: 100, y: 100, ease: Back.easeOut});
		targetpile.addEventListener(TouchEvent.TOUCH_DOWN, touchHandler);
		targetpile.addEventListener(TouchEvent.TOUCH_UP, finish)
		}
		
		private function photoPlace(targetphotoX:Number, targetphotoY:Number, targetphotoR:Number):void
		{
			
			if (targetInd < targetpile.numChildren)
			{
				targetPhoto = targetpile.getChildAt(targetInd) as TouchSprite;
				targetPhoto.addEventListener(TouchEvent.TOUCH_TAP, touchHandler);
				TweenLite.to(targetPhoto, 0.1, {x: targetphotoX, y: targetphotoY, rotation: targetphotoR, ease: Elastic.easeOut, onComplete: photoPlaceAgain});
				targetInd++
			}
		}
		
		private function photoPlaceAgain():void
		{
			
			
			photoPlace(targetphotoX, targetphotoY, targetphotoR);
		}
		
		private function touchHandler(e:TouchEvent):void
		{
			
		this.addEventListener(Event.ENTER_FRAME, frame)
	
		
		}
		private function frame(e:Event):void 
		{
			if (mouseX-mousePrevX>100 || mouseY-mousePrevY>100||mouseX-mousePrevX<-100 || mouseY-mousePrevY<-100) 
			{
	
				mousePrevX = Math.round(mouseX);
				mousePrevY = Math.round(mouseY);
				arrayX.push(mousePrevX);
				arrayY.push(mousePrevY);
			}
		}
		private function finish(e:TouchEvent):void 
		{
			this.removeEventListener(Event.ENTER_FRAME, frame)
			for (var i:int = 0; i < targetpile.numChildren; i++) 
			{
				targetPhoto = targetpile.getChildAt(i) as TouchSprite;
			//targetphotoX= Math.round(arrayX[i] - 100);
			//targetphotoY = Math.round(arrayY[i] - 100);
			
			targetphotoX= Math.round(arrayX[i] - 100) - (Math.round(arrayX[i] - 100)%100)
			targetphotoY= Math.round(arrayY[i] - 100) - (Math.round(arrayY[i] - 100)%100)
					TweenLite.to(targetPhoto, 1, { x: targetphotoX, y: targetphotoY, rotation:0, ease:Strong.easeInOut } );
					setTimeout(Aligment,2000)
			}
			
			mousePrevX = 0;
			mousePrevY = 0;
			arrayX = new Array
			arrayY = new Array
			
		}
		private function Aligment():void 
		{
			
				prevPhoto=targetpile.getChildAt(0) as TouchSprite
			for (var i:int = 1; i < targetpile.numChildren; i++) 
			{
				targetPhoto = targetpile.getChildAt(i) as TouchSprite;
				trace(targetPhoto.x);
				
				if (targetPhoto.x-prevPhoto.x>0) 
				{
					if (targetPhoto.x-prevPhoto.x>120||targetPhoto.x-prevPhoto.x<120&&targetPhoto.x-prevPhoto.x>40) 
					{
						targetPhoto.x=prevPhoto.x+120
					}
				}else 
				{
					if (targetPhoto.x-prevPhoto.x<0) 
					{
						if (targetPhoto.x-prevPhoto.x<-120||targetPhoto.x-prevPhoto.x>-120&&targetPhoto.x-prevPhoto.x<-40) 
					{
						targetPhoto.x=prevPhoto.x-120
					}
					}
					
				}
				
				///////////////////////////////////////////////////////////////////////////////
				/*	if (targetPhoto.y-prevPhoto.y>0) 
				{
					if (targetPhoto.y-prevPhoto.y>120||targetPhoto.y-prevPhoto.y<120&&targetPhoto.y-prevPhoto.y>40) 
					{
						targetPhoto.y=prevPhoto.y+120
					}
				}else 
				{
					if (targetPhoto.y-prevPhoto.y<0) 
					{
						if (targetPhoto.y-prevPhoto.y<-120||targetPhoto.y-prevPhoto.y>-120&&targetPhoto.y-prevPhoto.y<-40) 
					{
						targetPhoto.y=prevPhoto.y-120
					}
					}
					
				} */
				
				prevPhoto = targetPhoto
			}
		}

}
	
	}



